R 语言简介和应用领域
R 语言是一种用于统计计算和图形表示的编程语言和环境。它广泛应用于数据分析、统计建模、机器学习、数据可视化等领域。R 语言的强大之处在于其丰富的包(packages)生态系统,这些包提供了大量的统计和图形功能。
安装 R 和 RStudio
安装 R
- 访问 R 官方网站。
- 下载适用于你操作系统的 R 安装包。
- 按照安装向导完成安装。
安装 RStudio
- 访问 RStudio 官方网站。
- 下载适用于你操作系统的 RStudio 安装包。
- 按照安装向导完成安装。
R 基本语法
变量赋值和基本运算
# 变量赋值
x <- 5
y <- 10
# 基本运算
sum <- x + y
product <- x * y
print(sum) # 输出: 15
print(product) # 输出: 50
数据类型和结构
R 支持多种数据类型和结构,包括向量、矩阵、数据框和列表。
向量
# 创建向量
vec <- c(1, 2, 3, 4, 5)
print(vec) # 输出: 1 2 3 4 5
矩阵
# 创建矩阵
mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
print(mat)
# 输出:
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
数据框
# 创建数据框
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Salary = c(50000, 60000, 70000)
)
print(df)
# 输出:
# Name Age Salary
# 1 Alice 25 50000
# 2 Bob 30 60000
# 3 Charlie 35 70000
列表
# 创建列表
lst <- list(vec, mat, df)
print(lst)
# 输出:
# [[1]]
# [1] 1 2 3 4 5
#
# [[2]]
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
#
# [[3]]
# Name Age Salary
# 1 Alice 25 50000
# 2 Bob 30 60000
# 3 Charlie 35 70000
读取和写入 CSV 文件
读取 CSV 文件
# 读取 CSV 文件
data <- read.csv("data.csv")
print(data)
写入 CSV 文件
# 写入 CSV 文件
write.csv(df, "output.csv", row.names = FALSE)
读取 Excel 文件
使用 readxl 包
# 安装并加载 readxl 包
install.packages("readxl")
library(readxl)
# 读取 Excel 文件
excel_data <- read_excel("data.xlsx")
print(excel_data)
数据库连接 (SQL)
使用 DBI 和 RSQLite 包
# 安装并加载 DBI 和 RSQLite 包
install.packages("DBI")
install.packages("RSQLite")
library(DBI)
library(RSQLite)
# 连接到 SQLite 数据库
con <- dbConnect(RSQLite::SQLite(), "my_database.db")
# 执行 SQL 查询
result <- dbGetQuery(con, "SELECT * FROM my_table")
print(result)
# 关闭连接
dbDisconnect(con)
Web 数据抓取
使用 rvest 包
# 安装并加载 rvest 包
install.packages("rvest")
library(rvest)
# 抓取网页数据
url <- "https://example.com"
page <- read_html(url)
title <- page %>% html_node("title") %>% html_text()
print(title)
数据清洗
使用 dplyr 包
# 安装并加载 dplyr 包
install.packages("dplyr")
library(dplyr)
# 数据清洗示例
cleaned_data <- data %>%
filter(Age > 20) %>%
select(Name, Age)
print(cleaned_data)
缺失值处理
使用 tidyr 包
# 安装并加载 tidyr 包
install.packages("tidyr")
library(tidyr)
# 缺失值处理示例
data_filled <- data %>%
fill(Salary, .direction = "down")
print(data_filled)
数据重塑 (reshape2 包)
使用 reshape2 包
# 安装并加载 reshape2 包
install.packages("reshape2")
library(reshape2)
# 数据重塑示例
melted_data <- melt(data, id.vars = "Name")
print(melted_data)
数据合并和分组
使用 dplyr 包
# 数据合并示例
merged_data <- inner_join(data1, data2, by = "ID")
print(merged_data)
# 数据分组示例
grouped_data <- data %>%
group_by(Age) %>%
summarise(Mean_Salary = mean(Salary))
print(grouped_data)
基础图形函数
使用 plot 函数
# 基础图形示例
plot(data$Age, data$Salary, main = "Age vs Salary", xlab = "Age", ylab = "Salary")
ggplot2 包详解
使用 ggplot2 包
# 安装并加载 ggplot2 包
install.packages("ggplot2")
library(ggplot2)
# ggplot2 示例
ggplot(data, aes(x = Age, y = Salary)) +
geom_point() +
ggtitle("Age vs Salary")
交互式图表 (plotly)
使用 plotly 包
# 安装并加载 plotly 包
install.packages("plotly")
library(plotly)
# plotly 示例
plot_ly(data, x = ~Age, y = ~Salary, type = "scatter", mode = "markers")
地理数据可视化
使用 leaflet 包
# 安装并加载 leaflet 包
install.packages("leaflet")
library(leaflet)
# leaflet 示例
leaflet() %>%
addTiles() %>%
addMarkers(lng = -74.0060, lat = 40.7128, popup = "New York City")
描述性统计
使用 summary 函数
# 描述性统计示例
summary(data)
假设检验
使用 t.test 函数
# 假设检验示例
t_test_result <- t.test(data$Salary, mu = 60000)
print(t_test_result)
相关分析
使用 cor 函数
# 相关分析示例
correlation <- cor(data$Age, data$Salary)
print(correlation)
回归分析
使用 lm 函数
# 回归分析示例
model <- lm(Salary ~ Age, data = data)
summary(model)
方差分析 (ANOVA)
使用 aov 函数
# 方差分析示例
anova_result <- aov(Salary ~ Age, data = data)
summary(anova_result)
主成分分析 (PCA)
使用 prcomp 函数
# 主成分分析示例
pca_result <- prcomp(data[, c("Age", "Salary")], scale. = TRUE)
summary(pca_result)
聚类分析
使用 kmeans 函数
# 聚类分析示例
kmeans_result <- kmeans(data[, c("Age", "Salary")], centers = 3)
print(kmeans_result)
时间序列分析
使用 ts 函数
# 时间序列分析示例
ts_data <- ts(data$Salary, start = c(2020, 1), frequency = 12)
plot(ts_data)
监督学习 (分类, 回归)
使用 caret 包
# 安装并加载 caret 包
install.packages("caret")
library(caret)
# 监督学习示例
model <- train(Salary ~ Age, data = data, method = "lm")
print(model)
非监督学习 (聚类, 降维)
使用 kmeans 和 prcomp 函数
# 非监督学习示例
kmeans_result <- kmeans(data[, c("Age", "Salary")], centers = 3)
pca_result <- prcomp(data[, c("Age", "Salary")], scale. = TRUE)
print(kmeans_result)
print(pca_result)
模型评估和选择
使用 caret 包
# 模型评估和选择示例
model <- train(Salary ~ Age, data = data, method = "lm")
print(model)
交叉验证
使用 caret 包
# 交叉验证示例
train_control <- trainControl(method = "cv", number = 10)
model <- train(Salary ~ Age, data = data, trControl = train_control, method = "lm")
print(model)
文本预处理
使用 tm 包
# 安装并加载 tm 包
install.packages("tm")
library(tm)
# 文本预处理示例
corpus <- Corpus(VectorSource(data$Text))
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removeWords, stopwords("english"))
词频分析
使用 tm 包
# 词频分析示例
dtm <- DocumentTermMatrix(corpus)
freq <- colSums(as.matrix(dtm))
freq <- sort(freq, decreasing = TRUE)
print(freq)
情感分析
使用 syuzhet 包
# 安装并加载 syuzhet 包
install.packages("syuzhet")
library(syuzhet)
# 情感分析示例
sentiment <- get_sentiment(data$Text)
print(sentiment)
主题模型
使用 topicmodels 包
# 安装并加载 topicmodels 包
install.packages("topicmodels")
library(topicmodels)
# 主题模型示例
lda_model <- LDA(dtm, k = 3)
print(lda_model)
data.table 包
使用 data.table 包
# 安装并加载 data.table 包
install.packages("data.table")
library(data.table)
# data.table 示例
dt <- as.data.table(data)
result <- dt[, .(Mean_Salary = mean(Salary)), by = Age]
print(result)
dplyr 和 tidyr 包
使用 dplyr 和 tidyr 包
# dplyr 和 tidyr 示例
cleaned_data <- data %>%
filter(Age > 20) %>%
select(Name, Age) %>%
spread(key = Age, value = Name)
print(cleaned_data)
并行计算
使用 parallel 包
# 安装并加载 parallel 包
install.packages("parallel")
library(parallel)
# 并行计算示例
cl <- makeCluster(detectCores() - 1)
result <- parLapply(cl, 1:10, function(x) x^2)
stopCluster(cl)
print(result)
大规模数据集处理策略
使用 data.table 和 dplyr 包
# 大规模数据集处理示例
dt <- as.data.table(large_data)
result <- dt[, .(Mean_Salary = mean(Salary)), by = Age]
print(result)
R Markdown 基础
创建 R Markdown 文档
# 创建 R Markdown 文档
rmarkdown::render("my_document.Rmd")
创建动态报告
使用 knitr 包
# 安装并加载 knitr 包
install.packages("knitr")
library(knitr)
# 创建动态报告示例
knit("my_document.Rmd")
交互式文档 (Shiny)
使用 shiny 包
# 安装并加载 shiny 包
install.packages("shiny")
library(shiny)
# 创建 Shiny 应用示例
ui <- fluidPage(
titlePanel("My Shiny App"),
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 1, max = 100, value = 50)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs))
})
}
shinyApp(ui = ui, server = server)
学术论文和演示文稿制作
使用 rmarkdown 包
# 创建学术论文和演示文稿示例
rmarkdown::render("my_paper.Rmd", output_format = "pdf_document")
rmarkdown::render("my_presentation.Rmd", output_format = "ioslides_presentation")
函数定义和调用
定义和调用函数
# 定义函数
my_function <- function(x) {
return(x^2)
}
# 调用函数
result <- my_function(5)
print(result) # 输出: 25
匿名函数和闭包
匿名函数和闭包示例
# 匿名函数示例
square <- function(x) x^2
result <- square(5)
print(result) # 输出: 25
# 闭包示例
make_counter <- function() {
count <- 0
function() {
count <<- count + 1
return(count)
}
}
counter <- make_counter()
print(counter()) # 输出: 1
print(counter()) # 输出: 2
函数式编程概念
使用 purrr 包
# 安装并加载 purrr 包
install.packages("purrr")
library(purrr)
# 函数式编程示例
result <- map_dbl(1:5, ~ .x^2